#include "modelCommandTable.h"
#include "db.h"
#include "logger.h"
#include "version.h"
#include <string.h>
#include <stdlib.h>


#define MODEL_COMMAND_TABLE_NAME "t_model_command"
#define SQL_MODEL_COMMAND_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " MODEL_COMMAND_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, modelID INT, name TEXT NOT NULL, dataType INT, classify INT, value TEXT)"
#define SQL_MODEL_COMMAND_TABLE_INSERT(...) "INSERT INTO " MODEL_COMMAND_TABLE_NAME "(id, modelID, name, dataType, classify) VALUES (" #__VA_ARGS__ ")"
#define SQL_MODEL_COMMAND_TABLE_REPLACE(...) "REPLACE INTO " MODEL_COMMAND_TABLE_NAME "(id, modelID, name, dataType, classify) VALUES (" #__VA_ARGS__ ")"
#define SQL_MODEL_COMMAND_TABLE_DELETE(id) "DELETE FROM " MODEL_COMMAND_TABLE_NAME " WHERE id = " #id
#define SQL_MODEL_COMMAND_TABLE_SELECT(id) "SELECT * FROM " MODEL_COMMAND_TABLE_NAME " WHERE id = " #id


/*===============================================================*/
/* 模型测点 */
int createModelCommandPointTable(){
    return db_create_table(SQL_MODEL_COMMAND_TABLE_CREATE);
}

int insertModelCommandPoint(T_MODEL_COMMAND_POINT* modelCommand){
    if(NULL == modelCommand){
        log_file_e(APPNAME, "insert model command is NULL");
        return -1;
    }
    return db_insert(SQL_MODEL_COMMAND_TABLE_INSERT(modelCommand->id, modelCommand->modelID, modelCommand->name, modelCommand->dataType, modelCommand->classify, modelCommand->value));
}

int replaceModelCommandPoint(T_MODEL_COMMAND_POINT* modelCommand){
    if(NULL == modelCommand){
        log_file_e(APPNAME, "replace model command is NULL");
        return -1;
    }
    return db_insert(SQL_MODEL_COMMAND_TABLE_REPLACE(modelCommand->id, modelCommand->modelID, modelCommand->name, modelCommand->dataType, modelCommand->classify, modelCommand->value));
}

int deleteModelCommandPoint(T_MODEL_COMMAND_POINT* modelCommand){
    if(NULL == modelCommand){
        log_file_e(APPNAME, "delete model command is NULL");
        return -1;
    }
    return db_delete(SQL_MODEL_COMMAND_TABLE_DELETE(model->id));
}

static int modelCommandPointCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "modelCallback data is NULL");
        return -1;
    }
    T_MODEL_COMMAND_POINT_LIST* modelCommandList = (T_MODEL_COMMAND_POINT_LIST*)data;
    if((0 == modelCommandList->count) || (NULL == modelCommandList->head)){
        modelCommandList->head = modelCommandList->tail = (T_MODEL_COMMAND_POINT*)malloc(sizeof(T_MODEL_COMMAND_POINT));
        modelCommandList->head->next = NULL;
        modelCommandList->head->prev = NULL;
    } else {
        modelCommandList->tail->next = (T_MODEL_COMMAND_POINT*)malloc(sizeof(T_MODEL_COMMAND_POINT));
        modelCommandList->tail->next->prev = modelCommandList->tail;
        modelCommandList->tail = modelCommandList->tail->next;
    }
    modelCommandList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            modelCommandList->tail->id = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "modelID")){
            modelCommandList->tail->modelID = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "dataType")){
            modelCommandList->tail->dataType = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "classify")){
            modelCommandList->tail->classify = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "name")){
            if(NULL == argv[i]){
                modelCommandList->tail->name[0] = '\0';
            } else {
                strcpy(modelCommandList->tail->name, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "value")){
            if(NULL == argv[i]){
                modelCommandList->tail->value[0] = '\0';
            } else {
                strcpy(modelCommandList->tail->value, argv[i]);
            }
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return modelCommandList->count;
}

int selectModelCommandPoint(const char* sql, T_MODEL_COMMAND_POINT_LIST* modelCommandList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == modelCommandList)){
        log_file_e(APPNAME, "select sql is NULL");
        return -1;
    }
    return db_select(sql, modelCommandPointCallback, modelCommandList);

}
//
